** Preamble **
 
clear all
set more off

local user    "alexs"
local project "globo_debate"
local folder  "_replicate_me"

local path_input  "C:/Users/$user/Dropbox/$project/master/output/data"
local path_output "C:/Users/$user/Dropbox/$project/master/output/tables"

* Note: local directory is the output folder 
cd "`path_output'"

					** Loading the input file **


use 		 `path_input'/before.dta, clear
append using `path_input'/middle.dta
append using `path_input'/after.dta


drop if  metrop3==3 & wave==3
drop if  metrop2==3 & wave==2
drop if  metrop1==3 & wave==1

gen     MA=0
replace MA=1 if estad==16

gen T=0
replace T=1 if MA==0 & wave==3

decode estado, gen(uf)

merge m:1 uf using `path_input'/controls_RM

** Lula **
*W2_q2=Em quem vota no 2o turno
gen vota_lula_2oturno    = (W2_q2==1 | W3_q1==1 | W1_q2==1)

*W2_q3=Se o segundo turno fosse hoje
gen vota_lula_2oturno_v2 = (W2_q3==1 | W3_q2==1 | W1_q3==1)

*Ambos
gen vota_lula_2oturno_v3=vota_lula_2oturno*vota_lula_2oturno_v2

gen weird=0
replace weird=1 if W2_q2==0 | W3_q1==0 | W1_q2==0
replace weird=2 if W2_q2==3 | W3_q1==3 | W1_q2==3
replace weird=3 if W2_q2==4 | W3_q1==4 | W1_q2==4

gen ns=0
replace ns=1 if W2_q2==6 | W3_q1==6 | W1_q2==6


** Collor **
*W2_q2=Em quem vota no 2o turno
gen vota_collor_2oturno   = (W2_q2==2 | W3_q1==2 | W1_q2==2)

*W2_q3=Se o segundo turno fosse hoje
gen vota_collor_2oturno_v2= (W2_q3==2 | W3_q2==2 | W1_q3==2)

*Ambos
gen vota_collor_2oturno_v3=vota_collor_2oturno*vota_collor_2oturno_v2


*Turnout
gen     turnout    = (W2_q15==1 | W2_q15==2)
replace turnout=1 if (W1_q14==1 | W1_q14==2)
replace turnout=1 if (W3_q11==1 | W3_q11==2)

gen votou_1o_turno_LULA    = W2_q1==2
gen votou_1o_turno_BRIZOLA = W2_q1==3
gen votou_1o_turno_COVAS   = W2_q1==4
gen votou_1o_turno_MALUF   = W2_q1==5
gen votou_1o_turno_AFIF    = W2_q1==6
gen votou_1o_turno_ULISSES = W2_q1==7    
gen votou_1o_turno_FREIRE  = W2_q1==8 
gen votou_1o_turno_CHAVES  = W2_q1==9 
gen votou_1o_turno_CAIADO  = W2_q1==10 
gen votou_1o_turno_OTHER   = W2_q1==11 
   
*Assistiu o debate
gen watch3=0
replace watch3=1 if W3_q3<3 & wave==3
gen watch2=0
replace watch2=1 if W2_q11>0 & W2_q11<3 & wave==2 
gen watch=0
replace watch=1  if watch2==1
replace watch=1  if watch3==1
replace watch=.  if W2_q11==0
replace watch=.  if wave==1
drop watch2 watch3

*replace assistiu1a=0
*replace assistiu1a=1 if (W2_q11!=3) & wave==2
*replace assistiu1b=0
*replace assistiu1b=1 if (W2_q12>0) & wave==2

cap drop Lulawon*
gen Lulawon3=0
replace Lulawon3=1 if (W3_q4==1)  & wave==3
gen Lulawon2=0
replace Lulawon2=1 if (W2_q12==1) & wave==2 
gen Lulawon=0
replace Lulawon=1  if Lulawon2==1
replace Lulawon=1  if Lulawon3==1
*replace Lulawon=. if watch==0
replace Lulawon=.  if wave==1
*replace Lulawon=. if W3_q4==0
drop Lulawon2 Lulawon3

cap drop Collorwon*
gen Collorwon3=0
replace Collorwon3=1 if (W3_q4==2)  & wave==3
gen Collorwon2=0
replace Collorwon2=1 if (W2_q12==2) & wave==2 
gen Collorwon=0
replace Collorwon=1  if Collorwon2==1
replace Collorwon=1  if Collorwon3==1
*replace Collorwon=. if watch==0
replace Collorwon=.  if wave==1
*replace Collorwon=. if W3_q4==0
drop Collorwon2 Collorwon3
/*
cap drop DontKnow*
gen DontKnow3=0 if wave>2
replace DontKnow3=1 if (W3_q4==3 | W3_q4==4) & wave==3
gen DontKnow2=0 if wave>2 
replace DontKnow3=1 if (W2_q12==3 | W2_q12==4) & wave==2 
gen DontKnow=0
replace DontKnow=1 if DontKnow2==1
replace DontKnow=1 if DontKnow3==1
replace DontKnow=. if W2_q11==0
replace DontKnow=1 if Collorwon==0 & Lulawon==0
replace DontKnow=. if wave==1
*replace DontKnow=1 if W3_q4==0
drop DontKnow2 DontKnow3
*/


gen     titulo=0
replace titulo=1 if W1_q14==1 & wave==1
replace titulo=1 if W2_q15==1 & wave==2
replace titulo=1 if W3_q11==1 & wave==3

gen WW1_SEXO   = SEXO*(wave==1)
gen WW1_escola = escola*(wave==1)
gen WW1_IDADE  = IDADE*(wave==1)
gen WW2_SEXO   = SEXO*(wave==2)
gen WW2_escola = escola*(wave==2)
gen WW2_IDADE  = IDADE*(wave==2)
gen WW3_SEXO   = SEXO*(wave==3)
gen WW3_escola = escola*(wave==3)
gen WW3_IDADE  = IDADE*(wave==3)

drop if titulo==0

cap drop T1
foreach var in years_sch1991_rm renda1991_rm sh_tv1991_rm sh_agua1991_rm sh_elect1991_rm pop1991_rm{
gen T1`var'=`var'*(wave==3)
}
qui eststo: xi: reghdfe vota_lula_2oturno_v2 T T1*, vce(cluster estado) a(estado IDADE SEXO escola wave)
gen sample=e(sample)

keep if sample==1
estimates clear

gen       treatment = T 
label var treatment "Globo"
											
											** Table: Main results, survey data **
															
												 ** Panel B **
														
/*
The table reports regression estimates of the effect of Globo's edited coverage on Lula's and Collor's vote shares. Columns (1) and (2) report estimates using survey data. Globo's edited coverage of the debate was aired on the late evening of December 15th, 1989. Our survey data were collected on December 12th-15th and 16th, before and after coverage, respectively. Survey voting variables indicate 2nd-round vote intention for Lula and Collor. Columns (3) and (4) report the estimates using actual ballot data and include demographic characteristics interacted with year fixed effects as additional controls, repeating the baseline
specification of Columns (3) and (5) of Table 6 but restricting the sample to include only municipalities located in state-capital cities metropolitan areas to mimic the sampling of our survey data. Specifications in Columns (3) and (4) include metropolitan areas and time fixed effects. Heteroskedasticity-adjusted standard errors clustered at the state level for the survey data estimates and at the municipality level for the electoral estimates are reported below in brackets. Significantly different from zero at $99\% (***)$, $95\% (**)$ and $90\% (*)$ confidence level.
*/

local legend "The table reports regression estimates of the effect of Globo's edited coverage on Lula's and Collor's vote shares. Columns (1) and (2) report estimates using survey data. Globo's edited coverage of the debate was aired on the late evening of December 15th, 1989. Our survey data were collected on December 12th-15th and 16th, before and after coverage, respectively. Survey voting variables indicate 2nd-round vote intention for Lula and Collor. Columns (3) and (4) report the estimates using actual ballot data and include demographic characteristics interacted with year fixed effects as additional controls, repeating the baseline specification of Columns (3) and (5) of Table 6 but restricting the sample to include only municipalities located in state-capital cities metropolitan areas to mimic the sampling of our survey data. Specifications in Columns (3) and (4) include metropolitan areas and time fixed effects. Heteroskedasticity-adjusted standard errors clustered at the state level for the survey data estimates and at the municipality level for the electoral estimates are reported below in brackets. Significantly different from zero at $99\% (***)$, $95\% (**)$ and $90\% (*)$ confidence level."

estimates clear

eststo: xi: reghdfe vota_lula_2oturno_v2   treatment [aw=peso], vce(cluster estado) a(estado IDADE SEXO escola wave)
estadd local N_  = e(N)         
estadd local r2_ = round(e(r2), 0.001)         
estadd local DemControls "Yes"

eststo: xi: reghdfe vota_collor_2oturno_v2 treatment [aw=peso], vce(cluster estado) a(estado IDADE SEXO escola wave)
estadd local N_  = e(N)         
estadd local r2_ = round(e(r2), 0.001)         
estadd local DemControls "Yes"

eststo: xi: reghdfe vota_lula_2oturno_v2     		 [aw=peso], vce(cluster estado) a(estado IDADE SEXO escola wave)
estadd local N_          " "
estadd local r2_         " "
estadd local DemControls " "

eststo: xi: reghdfe vota_collor_2oturno_v2           [aw=peso], vce(cluster estado) a(estado IDADE SEXO escola wave)
estadd local N_          " "
estadd local r2_         " "
estadd local DemControls " "


gen sample_MSAs =e(sample)

esttab est1 est2 est3 est4 using Table_baseline_MSAs_panel_B.tex, 																				///
keep(treatment) order(treatment) 																												///
stat(N_ r2_ DemControls, fmt(0 2 0) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{S}{@}" "\multicolumn{1}{c}{@}") 							///
label("Observations" "R-squared" "Individual controls" )) 																				///
mgroups("Metropolitan areas" "Non-metropolitan areas" , pattern(1 0 1 0) span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cline{@span})) ///
star(* 0.10 ** 0.05 *** 0.01) 																													///
r2 b(3) replace  se(3) brac compress nonotes label  nogaps staraux nomtitles																	///
nonotes addnotes("\begin{minipage}{.8\linewidth} \footnotesize \smallskip \textbf{Note:} `legend' \end{minipage}" )	


include "https://raw.githubusercontent.com/steveofconnell/PanelCombine/master/PanelCombine.do"
panelcombine, use(Table_baseline_MSAs_panel_A.tex Table_baseline_MSAs_panel_B.tex) ///
paneltitles("Electoral data" "Survey data") columncount(5) save(Table_baseline_MSAs.tex)


panelcombine, use(Table_baseline_MSAs_panel_A.tex Table_baseline_MSAs_panel_B.tex) ///
paneltitles("Electoral data" "Survey data") columncount(5) save(effects_survey.tex) cleanup
 

